package com.beone.smart.smartBeauty.controller.appApi;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.beone.smart.smartBeauty.dao.DeviceInfoDao;
import com.beone.smart.smartBeauty.dao.PersonInfoDao;
import com.beone.smart.smartBeauty.model.entity.DeviceInfoDO;
import com.beone.smart.smartBeauty.model.entity.PersonInfoConfigDO;
import com.beone.smart.smartBeauty.model.form.BodyConfigCreateAndUpdateForm;
import com.beone.smart.smartBeauty.model.form.DeviceInfoForm;
import com.beone.smart.smartBeauty.model.result.ResultEntity;
import com.beone.smart.smartBeauty.model.result.ResultTool;
import com.beone.smart.smartBeauty.utils.LocalTimeUtil;
import com.beone.smart.smartBeauty.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created by bruce on 20/2/24 -- 16:35
 */
@Api(description = "移动app个人中心接口")
@Slf4j
@RestController
@RequestMapping(value = "/mobileAppPersonCenter")
public class AppPersonCenterController {
    /**
     * 个人中心	查询个人信息
     * 紧急联系人
     * 个人头像
     * 年龄设置
     */
    @Autowired
    private PersonInfoDao personInfoDao;
    @Autowired
    private DeviceInfoDao deviceInfoDao;

    @ApiOperation(value = "绑定设备信息", notes = "传递参数和设备中心的一致")
    @PostMapping(value = "/bindDeviceInfo")
    public ResultEntity bindDeviceInfo(@RequestBody DeviceInfoForm deviceInfoForm) {
        DeviceInfoDO deviceInfoDO = new DeviceInfoDO();
        BeanUtils.copyProperties(deviceInfoForm, deviceInfoDO);
        deviceInfoDO.setUserId(RequestUtil.getUid());
        deviceInfoDO.setUserName(RequestUtil.getUserName());
        deviceInfoDO.setJoinDate(LocalTimeUtil.getDateTime());
        //
        deviceInfoDao.insert(deviceInfoDO);
        return ResultTool.ok(deviceInfoDO);
    }

    @ApiOperation(value = "解除绑定设备信息", notes = "传递参数和设备中心的一致")
    @PostMapping(value = "/unbindDeviceInfo")
    public ResultEntity unbindDeviceInfo(@RequestBody DeviceInfoForm deviceInfoForm) {
        return ResultTool.ok(deviceInfoForm);
    }

    /**
     * 个人信息配置
     */
    @ApiOperation(value = "个人信息配置，比如年龄体重、身高设置")
    @PostMapping(value = "/personInfoConfig")
    public ResultEntity personInfoConfig(@RequestBody BodyConfigCreateAndUpdateForm bodyConfigCreateAndUpdateForm) {
        PersonInfoConfigDO personInfoConfigDO = new PersonInfoConfigDO();
        BeanUtils.copyProperties(bodyConfigCreateAndUpdateForm, personInfoConfigDO);
        String uid = RequestUtil.getUid();
        //config user ID and update or create datetime
        personInfoConfigDO.setConfigDate(LocalTimeUtil.getDateTime());
        personInfoConfigDO.setUserId(uid);
        if (StringUtils.isEmpty(personInfoConfigDO.getId())) {
            LambdaQueryWrapper<PersonInfoConfigDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PersonInfoConfigDO::getUserId, uid);
            PersonInfoConfigDO result = personInfoDao.selectOne(queryWrapper);
            if (result != null) {
                personInfoConfigDO.setId(result.getId());
                personInfoDao.updateById(personInfoConfigDO);
            } else {
                personInfoDao.insert(personInfoConfigDO);
            }
        } else {
            personInfoDao.updateById(personInfoConfigDO);
        }
        return ResultTool.ok(personInfoConfigDO);
    }

    /**
     * 个人信息查询
     */
    @ApiOperation(value = "个人信息查询")
    @GetMapping(value = "/personInfoConfigQuery")
    public ResultEntity personInfoConfigQuery() {
        String uid = RequestUtil.getUid();
        LambdaQueryWrapper<PersonInfoConfigDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PersonInfoConfigDO::getUserId, uid);
        PersonInfoConfigDO personInfoConfigDO = personInfoDao.selectOne(queryWrapper);
        return ResultTool.ok(personInfoConfigDO);
    }
}
